﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using UnityEngine;
using Warensoft.Unity.Communication.Client;
using Warensoft.Unity.Communication.Client.Contracts;
using Warensoft.Unity.Communication.Client.Models;


public class GetHttpResourceTest:MonoBehaviour
{
    private Texture2D image;
    UnityHttpClient client;
    public void Start()
    {
        client = UnityCommunicationManager.CreateInstance().GetHttpClient();
        //handles http errors
        client.Error += client_Error;
    }

    void client_Error(object sender, HttpRequestErrorEventArgs e)
    {
        //output http status code
        print(e.Status);
        //output http response text with errors
        print(e.ResponseText );
    }

    public void Update()
    {
        
        //Get string resources
        client.BeginGetHttpContent("http://www.abc.com/default.aspx?id=1",new Action<string>((stringResult) => 
        {
            print(stringResult);
        }));
        //Get xml resources
        client.BeginGetHttpContent("http://www.abc.com/test.xml", new Action<XmlDocument>((doc) => 
        {
            print(doc.DocumentElement.ChildNodes[0].InnerXml);
        }));
        //Get image resources
        client.BeginGetHttpContent("http://www.abc.com/test.png", new Action<Texture2D>((img) => 
        {
            this.image = img;
        }));
        //Get Binary resources
        client.BeginGetHttpContent("http://www.abc.com/myapp.exe", new Action<byte[]>((resultBuffer) =>
        {
            using (FileStream fs=new FileStream ("d:\\myapp.exe",FileMode.Create))
            {
                fs.Write(resultBuffer ,0,resultBuffer .Length);
            }
        }));
    }
}

/// <summary>
/// custom post data class
/// </summary>
public class MyDataClass:IMessage
{

    public string Name { get; set; }
    public int Age { get; set; }
    public bool Gender { get; set; }

    public byte[] ToArray()
    {
        byte[] buffer = null;
        using (MemoryStream ms=new MemoryStream ())
        {
            using (BinaryWriter bw=new BinaryWriter (ms))
            {
                bw.Write(this.Name);
                bw.Write(this.Age);
                bw.Write(this.Gender);
            }
        }
        return buffer;
    }
}
/// <summary>
/// custom response message class
/// </summary>
public class MyResponse:IResponseMessage
{
    public bool OperationDone { get; set; }
    public string OperationMessage { get; set; }
    public void FillData(byte[] buffer)
    {
        using (MemoryStream ms=new MemoryStream ())
        {
            using (BinaryReader br=new BinaryReader (ms))
            {
                this.OperationDone = br.ReadBoolean();
                this.OperationMessage = br.ReadString();
            }
        }
    }
}
public class PostDataTest:MonoBehaviour
{
    UnityHttpClient client;
    public void Start()
    {
        this.client = UnityCommunicationManager.CreateInstance().GetHttpClient();
    }

    public void Update()
    {
        MyDataClass dc=new MyDataClass ()
        {
            Name ="warensoft",
            Age=10,
            Gender=true
        };
        this.client.BeginPost("http://www.abc.com/default.aspx",dc,new Action<HttpResponse>((response)=>
        {
            //get the responsed bytes
            byte[] buffer = response.Data;
            //cast the responsed bytes to specified type
            MyResponse responsemessage = response.GetData<MyResponse>();
        }));
    }

}